package csel.model;

import java.io.*;

class SerializationLoaderSaver implements LoaderSaver
{

	public SerializationLoaderSaver()
	{

	}

	public GameData load(String filepath) throws FileNotFoundException,
			IOException
	{
		FileInputStream fileIn;
		ObjectInputStream serializer;
		GameData data;

		fileIn = new FileInputStream(filepath);
		serializer = new ObjectInputStream(fileIn);

		try
		{
			data = (GameData) serializer.readObject();
		} catch (ClassNotFoundException ex)
		{
			throw new IOException("Invalid save file data.", ex);
		}

		/*
		 * GC would call these, but I'll call now to avoid locking the file any
		 * longer than necessary.
		 */
		serializer.close();
		fileIn.close();

		return data;
	}

	public void save(String filepath, GameData data)
			throws FileNotFoundException, IOException
	{
		FileOutputStream fileOut = new FileOutputStream(filepath);
		ObjectOutputStream serializer = new ObjectOutputStream(fileOut);

		serializer.writeObject(data);

		/*
		 * GC would call these, but I'll call now to avoid locking the file any
		 * longer than necessary.
		 */
		serializer.close();
		fileOut.close();
	}

}
